<?php

namespace app\api\model;

use think\Model;


class Papers extends Model
{

    // 表名
    protected $name = 'exam_paper_images';
    
    // 自动写入时间戳字段
     protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
    ];
    
    //一键考试统分
    public static function totalScore($id)
    {
        $data = [];
        $group = db('exam_paper_images')->where(['page'=>1,'status'=>3])->where('exams_id',$id)->field('class_id,grade_id,exams_list_id,exams_id,school_id,student_id,student_name,exam_number,stu_number')->group('student_id')->select();
 
        foreach ($group as $key => $value) {
            
            //主观题得分
            $zhuscore = db('exam_teacher_make_log')->where(['exams_id'=>$value['exams_id'],'student_id'=>$value['student_id']])->where('type','in',[1,4,3,5])->sum('bscore');
            //客观题得分
            $kescore = db('exam_paper_images')->where(['exams_id'=>$value['exams_id'],'student_id'=>$value['student_id']])->sum('scoresum');
            //总分
            $totalScore = bcadd($kescore,$zhuscore,0);
            //科目id
            $subject_id = db('exam_list')->where('id',$value['exams_list_id'])->value('subject_id');
            $data[$key] = [
                'school_id' => $value['school_id'],
                'exams_id' => $value['exams_id'],
                'student_id' => $value['student_id'],
                'grade_id' => $value['grade_id'],
                'class_id' => $value['class_id'],
                'subject_id' => $subject_id,
                'student_name' => $value['student_name'],
                'exam_number' => $value['exam_number'],
                'stu_number' => $value['stu_number'],
                'totalscore' => $totalScore,
                'status' => 0,
                'createtime' => time()
            ];
        }
        //按学校分组
        $schoolData = array_group_by($data, 'school_id');
        foreach ($schoolData as $school) {
            $school = arraySort($school,'totalscore');
            foreach ($school as $num =>$val) {
                foreach ($data as &$valu) {
                    if ($val['student_id'] == $valu['student_id']) {
                        $valu['schoolrank'] = $num+1;
                    }
                }
            }
        }
        //按年级
        $gradeData = array_group_by($data, 'grade_id');
        foreach ($gradeData as $grade) {
            $grade = arraySort($grade,'totalscore');
            foreach ($grade as $num =>$val) {
                foreach ($data as &$vale) {
                    if ($val['student_id'] == $vale['student_id']) {
                        $vale['graderank'] = $num+1;
                    }
                }
                
            }
        }

        //按班级
        $classData = array_group_by($data, 'class_id');
        foreach ($classData as $class) {
            $class = arraySort($class,'totalscore');
            foreach ($class as $num =>$val) {
                foreach ($data as &$cla) {
                    if ($val['student_id'] == $cla['student_id']) {
                        $cla['classrank'] = $num+1;
                    }
                }
                
            }
        }
        return $data;
    }

    //一键考试统分
    public static function totalScoreNew($id)
    {
        $data = [];
        $group = db('exam_paper_images')->where(['page'=>1,'status'=>3])->where('exams_id',$id)->field('class_id,grade_id,exams_list_id,exams_id,school_id,student_id,student_name,exam_number,stu_number')->group('student_id')->select();
 
        foreach ($group as $key => $value) {
            //总分
            $totalScore = db('exam_paper_student_list')->where(['exams_id'=>$value['exams_id'],'student_id'=>$value['student_id']])->sum('totalscore');
            //科目id
            $subject_id = db('exam_list')->where('id',$value['exams_list_id'])->value('subject_id');
            $data[$key] = [
                'school_id' => $value['school_id'],
                'exams_id' => $value['exams_id'],
                'student_id' => $value['student_id'],
                'grade_id' => $value['grade_id'],
                'class_id' => $value['class_id'],
                'subject_id' => $subject_id,
                'student_name' => $value['student_name'],
                'exam_number' => $value['exam_number'],
                'stu_number' => $value['stu_number'],
                'totalscore' => $totalScore,
                'status' => 0,
                'createtime' => time()
            ];
        }
        
        return $data;
    }

    //单科统分
    public static function subjectScore($ids)
    {
        $data = [];
        $list = db('exam_paper_images')->where(['page'=>1,'status'=>3])->where('exams_list_id','in',$ids)->select();
        
        foreach ($list as $key => $value) {
           
            //主观题得分
            $zhuscore = db('exam_teacher_make_log')->where(['exams_list_id'=>$value['exams_list_id'],'student_id'=>$value['student_id']])->where('type','in',[1,4,3,5])->sum('bscore');
            //总分
            $totalScore = bcadd($value['scoresum'],$zhuscore,0);
            //科目id
            $subject_id = db('exam_list')->where('id',$value['exams_list_id'])->value('subject_id');
            $data[$key] = [
                'school_id' => $value['school_id'],
                'exams_id' => $value['exams_id'],
                'exams_list_id' => $value['exams_list_id'],
                'student_id' => $value['student_id'],
                'grade_id' => $value['grade_id'],
                'class_id' => $value['class_id'],
                'subject_id' => $subject_id,
                'student_name' => $value['student_name'],
                'exam_number' => $value['exam_number'],
                'stu_number' => $value['stu_number'],
                'kscoresum' => $value['scoresum'],
                'zscoresum' => $zhuscore,
                'totalscore' => $totalScore,
                'status' => 0,
                'createtime' => time()
            ];
        }
        
        return $data;
    }

    //单科统分
    public static function subjectScoreNew($ids)
    {
        $data = [];
        $list = db('exam_paper_images')->where(['page'=>1,'status'=>3])->where('exams_list_id','in',$ids)->select();
        
        foreach ($list as $key => $value) {
           
            //主观题得分
            $zhuscore = MakeLog::calculateStudentScore($value['exams_list_id'],$value['student_id']);
            //总分
            $totalScore = bcadd($value['scoresum'],$zhuscore,0);
            //科目id
            $subject_id = db('exam_list')->where('id',$value['exams_list_id'])->value('subject_id');
            $data[$key] = [
                'school_id' => $value['school_id'],
                'exams_id' => $value['exams_id'],
                'exams_list_id' => $value['exams_list_id'],
                'student_id' => $value['student_id'],
                'grade_id' => $value['grade_id'],
                'class_id' => $value['class_id'],
                'subject_id' => $subject_id,
                'student_name' => $value['student_name'],
                'exam_number' => $value['exam_number'],
                'stu_number' => $value['stu_number'],
                'kscoresum' => $value['scoresum'],
                'zscoresum' => $zhuscore,
                'totalscore' => $totalScore,
                'status' => 0,
                'createtime' => time()
            ];
        }
        
        return $data;
    }

    /**
     * 排名操作
     *
     * @param array $data 待处理数组
     * @param string $field 排序字段
     * @param string $model 
     * @param string $up 更新字段
     * @return void
     */
    public static function setRank($data,$field,$model,$up)
    {
        $classData = array_group_by($data, $field);
        foreach ($classData as $class) {
            $class = arraySort($class,'totalscore');
            foreach ($class as $num =>$val) {
                foreach ($data as $value) {
                    if ($val['student_id'] == $value['student_id']) {
                        db($model)->where(['id' => $value['id']])->update([
                            $up => $num + 1
                        ]);
                    }
                }
                
            }
        }
    }


    //计算客观题得分
    public function computeScore($id)
    {
        $list = db('exam_paper_images')->where(['exams_id'=>$id,'page'=>1,'status'=>3])->select();
        if (empty($list)) return false;
        //客观题得分
        $scoresum = 0;
        foreach ($list as $keys => $value) {
            if(!$value['kanswer']) return false;
            $kanswer = json_decode($value['kanswer'],true);
            $data = [];
            foreach ($kanswer['choiceAnswers'] as $key => $answer) {
                $exams_list = db('exam_paper_question_list')->where(['exams_list_id' => $value['exams_list_id'], 'key' => $answer['qsnum'], 'type' => 'KG'])->field('id,name,key,answer,score')->find();
                if ($exams_list['answer'] && $answer['selAnswer']) {
                    if ($exams_list['answer'] === $answer['selAnswer']) {
                        $score = $exams_list['score'];
                        $scoresum = bcadd($scoresum, $score, 0);
                        $restult = 'true';
                    } else {
                        $score = 0;
                        $restult = 'false';
                    }
                    $data[$key] = [
                        'exams_id' => $value['exams_id'],
                        'exams_list_id' => $value['exams_list_id'],
                        'student_id' => $value['student_id'],
                        'school_id' => $value['school_id'],
                        'key' => $answer['qsnum'],
                        'answer' => $answer['selAnswer'],
                        'restult' => $restult,
                        'name' => $exams_list['name'],
                        'score' => $score,
                        'rightScore' => $exams_list['score'],
                        'rightAnswer' => $exams_list['answer']
                    ];
                }
                
            }
            if (!empty($data)) {
                db('exam_paper_images')->where('id', $value['id'])->update([
                    'kanswerresult' => json_encode($data),
                    'scoresum' => $scoresum
                ]);
            } 
        }
        return true;
    }
    

}
